package com.gxa.controller.commnotice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.Result;


import com.gxa.entity.commnotice.Notice;
import com.gxa.entity.commnotice.Query;
import com.gxa.service.commnotice.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(tags = "社区通知")
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("公告通知列表接口")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo> seletAll(@PathVariable("pageNum") Integer pageNum,
                                     @PathVariable("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);

        PageInfo<Notice> pageInfo = new PageInfo<>(noticeService.seletAll());

        return Result.success(pageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("添加通知")
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice){
        int results = noticeService.add(notice);
        if (results == 1){
            return Result.success("add success");
        }else {
            return Result.failed("add failed");
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除通知")
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id") Integer id){
        for(Notice n:noticeService.seletAll()){
            if(n.getId()== id){
                int results = noticeService.delete(id);
                return Result.success("del Success");
            }
        }
        return Result.failed("del failed");
    }


    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("按条件查询通知")
    @PostMapping("/query")
    public Result<PageInfo> query(@RequestBody Query query){
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        PageInfo<Notice> pageInfo = new PageInfo<>(noticeService.queryByCondition(query));
        return Result.success(pageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("查询所有的状态")
    @GetMapping("/queryByNoticeState")
    public Result<HashMap> queryBy(){
        HashMap<Integer,String> noticeStateNames = new HashMap<>();
        noticeStateNames.put(0,"发布中");
        noticeStateNames.put(1,"已完成");
        noticeStateNames.put(2,"已撤销");
        return Result.success(noticeStateNames);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据id查询")
    @GetMapping("/queryById/{id}")
    public Result<Notice> queryById(@PathVariable("id") Integer id){
        for(Notice n:noticeService.seletAll()){
            if (id == n.getId()){
                return Result.success(n);
            }
        }
        return Result.failed("query error");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("修改消息状态")
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice){
        int results = this.noticeService.update(notice);
        if (results == 1){
            return Result.success("update success");
        }else {
            return Result.failed("update failed");
        }
    }
}
